package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description：
 * User：user
 * DATE:2021-10-27
 * Time:14:40
 */
class Animal{
    public String name;
    public int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat(){
        System.out.println(name + "eat()");
    }
}
/*class Dog{
    public String name;
    public int age;
    public  void eat(){
        System.out.println(name+"Dog::eat()");
    }
    public void bark(){
        System.out.println(name+"Dog::bark()");
    }
}
class Bird {
    public String name;
    public int age;
    public  void eat(){
        System.out.println(name+"Bird:: eat()");
    }
    public void fly(){
        System.out.println(name+"Bird::fay()");
    }
}*/

class Dog extends  Animal{
    public String name = "ff";//优先调用自己的的
    public Dog(String name,int age){
        super(name,age);
    }
    /*public Dog(){
        super();
    }默认生成的一个方法
    */
    public  void test(){
        super.eat();
        System.out.println(name);
        System.out.println(super.name);
    }
    public void eat(){
        System.out.println("dag eat()");
    }
}
class Bird extends  Animal{
    public  String selfFiled;
    public Bird(String name,int age,String selfFiled){
        super("hah",10);
        this.selfFiled = selfFiled;
    }
    public void fly(){
        System.out.println(name+"Bird::fly()");
    }
}
public class TestDemo {

    public static void func(Animal animal){
        animal.eat();
    }
    public static  Animal func2(){
        Dog dog = new Dog("HH",10);
        return dog;
    }

    public static void main(String[] args) {
        Dog dog = new Dog("HH",10);
        func(dog);
        Bird bird = new Bird("HH",10,"dd");
        func(bird);
    }
    public static void main6(String[] args) {
        Animal animal1 = new Dog("HH",10);
        animal1.eat();//此处运行时绑定 输出是dog eat() 而不是dog eat()
        Animal animal2 = new Bird("小鸟",10,"ee");
        animal2.eat();
    }

    public static void main4(String[] args) {
        //向下转型
        Animal animal = new Bird("小鸟",18,"sel");
        animal.eat();
        //向下转型
        if(animal instanceof  Bird){
            Bird bird = (Bird) animal;
            bird.fly();
        }


        //向下转型不安全
        Animal animal1 = new Dog("dog",12);
        //判断是否是Bird的实例
        if(animal instanceof  Bird){
            Bird bird2 = (Bird) animal;
            bird2.fly();
        }
    }
    public static void main3(String[] args) {
        /*Dog dog = new Dog("小狗",10);
        Animal animal = dog;*/
        //向上转型：父类引用引用子类对象
        Animal animal1 = new Dog("HH",10);
        animal1.eat();
        Animal animal2 = new Bird("小鸟",10,"ee");
        animal2.eat();
//        animal2.fly();    无法访问子类特有的对象
//        animal2.selFiled; 无法访问子类特有的对象
    }
 /*   public static void main2(String[] args) {
        Bird bird = new Bird();
        bird.name = "小鸟";
        bird.eat();
    }
    public static void main1(String[] args) {
        Dog dog = new Dog();
        dog.name = "长江7号";
        dog.eat();
    }*/
}







